﻿Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Text

Imports Tohto.Base.ExceptionPolicy.Config
Imports Tohto.Base.ExceptionPolicy.Interfaces

Namespace LogHandler
    Public Class DefaultLogHandler
        Implements IExceptionLogHandler

        Private Function GetLogInfoSequence() As String
            Return String.Empty
        End Function

        Private Sub InsertLogInfo(ByVal serial_id As String, ByVal excType As String, ByVal excMsg As String, ByVal excPosition As String, ByVal userID As String)

        End Sub

        Public Function ExLog(ByVal ex As Exception, ByVal strErrorCode As String, ByVal strLayerName As String, ByVal strUserId As String, ByVal config As ExceptionConfig.LogHandlerConfig) As Integer Implements Interfaces.IExceptionLogHandler.ExLog

            Dim strLogID As String = "0"
            'strLogID = Me.GetLogInfoSequence()
            WriteTextLog(ex, strUserId)

            'Dim strExceptionMsg As String = ""

            'Dim iposExcType As Integer = 0
            'Dim iposExcMsg As Integer = 0
            'Dim iposExcSource As Integer = 0
            'Dim iposExcLoc As Integer = 0

            'Dim ExcType As String = ""
            'Dim ExcMsg As String = ""
            'Dim ExcLocation As String = ""


            'strExceptionMsg = ex.Message.ToString()
            'strExceptionMsg = strLayerName & strExceptionMsg



            'If strExceptionMsg.IndexOf("Exceptionタイプ:") > -1 Then
            '    iposExcType = strExceptionMsg.IndexOf("Exceptionタイプ:")
            'End If

            'If strExceptionMsg.IndexOf("Exceptionメッセージ:") > -1 Then
            '    iposExcMsg = strExceptionMsg.IndexOf("Exceptionメッセージ:")
            'End If

            'If strExceptionMsg.IndexOf("Exceptionソース:") > -1 Then
            '    iposExcSource = strExceptionMsg.IndexOf("Exceptionソース:")
            'End If

            'If strExceptionMsg.IndexOf("Exceptionの所:") > -1 Then
            '    iposExcLoc = strExceptionMsg.IndexOf("Exceptionの所")
            'End If

            'If iposExcMsg - iposExcType - 1 > 0 Then
            '    ExcType = strExceptionMsg.Substring(iposExcType, iposExcMsg - iposExcType - 1).Trim()
            'End If
            'If iposExcSource - iposExcMsg - 1 > 0 Then
            '    ExcMsg = strExceptionMsg.Substring(iposExcMsg, iposExcSource - iposExcMsg - 1).Trim()
            'End If

            'ExcLocation = strExceptionMsg.Substring(iposExcLoc)

            'Me.InsertLogInfo(strLogID, ExcType, ExcMsg, ExcLocation, strUserId)
            Return Convert.ToInt32(strLogID)
        End Function

        Public Function ExLog1(ByVal ex As System.Exception, ByVal strErrorCode As String, ByVal strLayerName As String, ByVal strUserId As String, ByVal config As Config.ExceptionConfig.LogHandlerConfig) As Integer
            Return Nothing
        End Function

        ''' <summary>
        ''' エラーログを出力します
        ''' </summary>
        ''' <param name="ex">エラーオブジェクト</param>
        ''' <param name="strUserId">ユーザーコード</param>
        Private Sub WriteTextLog(ByVal ex As System.Exception, ByVal strUserId As String)
            Dim sw As StreamWriter = Nothing
            Try

                Dim path As String = AppDomain.CurrentDomain.BaseDirectory & "System\Log\"

                If Not Directory.Exists(path) Then
                    Directory.CreateDirectory(path)
                End If

                Dim fileFullPath As String = path & DateTime.Now.ToString("yyyy-MM-dd") & ".System.txt"

                Dim str As New StringBuilder

                str.AppendLine("************************************************************************")
                str.AppendLine("時間：" & DateTime.Now.ToString())
                str.AppendLine("ユーザーId：" & strUserId)
                str.AppendLine("エラーメッセージ：" & ex.Message.ToString())
                str.AppendLine("スタック:")
                str.AppendLine("　　" & ex.ToString())
                str.AppendLine("************************************************************************")
                str.AppendLine()


                If Not File.Exists(fileFullPath) Then
                    sw = File.CreateText(fileFullPath)
                Else
                    sw = File.AppendText(fileFullPath)
                End If

                sw.WriteLine(str.ToString())
            Finally
                If Not sw Is Nothing Then
                    sw.Close()
                    sw.Dispose()
                End If
            End Try
        End Sub
    End Class
End Namespace
